package com.zygh.dlqwservice.util;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zygh.dlqwservice.common.en.Constant;
import com.zygh.dlqwservice.common.en.Month;
import com.zygh.dlqwservice.module.entity.Sign;
import com.zygh.dlqwservice.module.service.ISignService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 自动签退类
 * @author lee
 */
@Component
public class SignOutSchedul {
    @Resource
    private ISignService iSignService;

    /**
     * 高峰岗强制签退(早高峰)
     */
    @Scheduled(cron = "0 30 9 * * ?")
    public void signMorning(){
        //高峰岗冬季夏季都是8:30签退，提后一小时签退
        List<Sign> signs = iSignService.list(Wrappers.<Sign>lambdaQuery()
                .isNull(Sign::getSignouttime)
                .eq(Sign::getJobName,"高峰岗"));
        for(Sign sign:signs){
            sign.setSignouttime(DateUtil.now());
            sign.setState(2);
        }
        iSignService.saveOrUpdateBatch(signs);
    }

    /**
     * 高峰岗夏季签退(晚高峰)
     */
    @Scheduled(cron = "0 0 20 * * ?")
    public void signNight(){
        String season = getSeason();
        //如果是冬季，提前结束
        if(season.contains(Constant.Season.Winter.getVal())){
            return;
        }
        List<Sign> signs = iSignService.list(Wrappers.<Sign>lambdaQuery()
                .isNull(Sign::getSignouttime)
                .eq(Sign::getJobName, "高峰岗"));
        for(Sign sign:signs){
            sign.setSignouttime(DateUtil.now());
            sign.setState(2);
        }
        iSignService.saveOrUpdateBatch(signs);
    }

    /**
     * 高峰岗冬季签退(晚高峰)
     */
    @Scheduled(cron = "0 30 19 * * ?")
    public void signNightWinner(){
        String season = getSeason();
        //如果是夏季，提前结束
        if(season.contains(Constant.Season.Summer.getVal())){
            return;
        }
        List<Sign> signs = iSignService.list(Wrappers.<Sign>lambdaQuery()
                .isNull(Sign::getSignouttime)
                .eq(Sign::getJobName, "高峰岗"));
        for(Sign sign:signs){
            sign.setSignouttime(DateUtil.now());
            sign.setState(2);
        }
        iSignService.saveOrUpdateBatch(signs);
    }
    /**
     * 平峰岗签退下午班网格，固定，铁骑，机动
     */
    @Scheduled(cron = "0 0 19 * * ?")
    public void signDaily(){
        List<Sign> signs = iSignService.list(Wrappers.<Sign>lambdaQuery()
                .isNull(Sign::getSignouttime)
                .nested(x ->
                    x.eq(Sign::getJobName, "网格")
                            .or()
                            .eq(Sign::getJobName, "固定")
                            .or()
                            .eq(Sign::getJobName, "铁骑")
                            .or()
                            .eq(Sign::getJobName, "机动")
                ));
        for(Sign sign:signs){
            sign.setSignouttime(DateUtil.now());
            sign.setState(2);
        }
        iSignService.saveOrUpdateBatch(signs);
    }
    /**
     * 平峰岗签退上午班网格，固定，铁骑，机动
     */
    @Scheduled(cron = "0 0 14 * * ?")
    public void signDailyMorning(){
        List<Sign> signs = iSignService.list(Wrappers.<Sign>lambdaQuery()
                .isNull(Sign::getSignouttime)
                .nested(x ->
                        x.eq(Sign::getJobName, "网格")
                                .or()
                                .eq(Sign::getJobName, "固定")
                                .or()
                                .eq(Sign::getJobName, "铁骑")
                                .or()
                                .eq(Sign::getJobName, "机动")
                ));
        for(Sign sign:signs){
            sign.setSignouttime(DateUtil.now());
            sign.setState(2);
        }
        iSignService.saveOrUpdateBatch(signs);
    }
    /**
     * 夜巡签退
     */
    @Scheduled(cron = "0 30 8 * * ?")
    public void signCruiser(){
        List<Sign> signs = iSignService.list(Wrappers.<Sign>lambdaQuery()
                .isNull(Sign::getSignouttime)
                .eq(Sign::getJobName, "夜巡"));
        for(Sign sign:signs){
            sign.setSignouttime(DateUtil.now());
            sign.setState(2);
        }
        iSignService.saveOrUpdateBatch(signs);
    }
    /**
     * 判断冬季还是夏季
     * @return 季节
     */
    private String getSeason(){
        int month = DateUtil.thisMonth()+1;
        Month monthEnum = Month.getTypeByValue(month);
        return monthEnum.getSeason();
    }
}
